// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HorizontalDepthProfileModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Visualization
{
    using System.Runtime.Serialization;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Microsoft.Robotics.Navigation.Perception;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// Generates a model for visualization of a horizontal depth profile. The visualization is a top-down view of a depth image.
    /// </summary>
    /// <typeparam name="THorizontalDepthProfile">The kind of horizontal depth profile to visualize</typeparam>
    [DataContract]
    public sealed class HorizontalDepthProfileModelAgent<THorizontalDepthProfile> : ModelAgent<SimpleAgentMessage<THorizontalDepthProfile>, HorizontalDepthProfileModel>
        where THorizontalDepthProfile : IHorizontalDepthProfile
    {
        /// <summary>
        /// Default cutoff depth
        /// </summary>
        private const double DefaultMaximumDepth = 10.0;

        /// <summary>
        /// Default pixel-to-world mapping
        /// </summary>
        private const double DefaultResolutionInMetersPerPixel = 0.02;

        /// <summary>
        /// The cutoff depth for observations in the hdp
        /// </summary>
        [DataMember]
        private double maximumDepth;

        /// <summary>
        /// The pixel-to-world mapping in the rendered image 
        /// </summary>
        [DataMember]
        private double resolutionInMetersPerPixel;
        
        /// <summary>
        /// Initializes a new instance of the HorizontalDepthProfileModelAgent class.
        /// This constructor is only called during manifest creation time.
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>        
        /// <param name="maximumDepth">The cutoff depth for observations in the hdp</param>
        /// <param name="resolutionInMetersPerPixel">The pixel-to-world mapping in the rendered image </param>
        public HorizontalDepthProfileModelAgent(
            string name,
            IProducer<SimpleAgentMessage<THorizontalDepthProfile>> producer,
            int maxUpdateIntervalMilliseconds,
            double maximumDepth,
            double resolutionInMetersPerPixel)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
            this.maximumDepth = maximumDepth;
            this.resolutionInMetersPerPixel = resolutionInMetersPerPixel;
        }

        /// <summary>
        /// Called upon agent creation in-lieu of a constructor
        /// </summary>
        /// <param name="locator">The locator that provides access to loaded agents</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            if (this.resolutionInMetersPerPixel == 0)
            {
                this.resolutionInMetersPerPixel = DefaultResolutionInMetersPerPixel;
            }

            if (this.maximumDepth == 0)
            {
                this.maximumDepth = DefaultMaximumDepth;
            }
        }

        /// <summary>
        /// Update the model when a new local map is received
        /// </summary>
        /// <param name="message">State containing a local map</param>
        /// <returns>True if the model was updated</returns>
        protected override bool UpdateModel(SimpleAgentMessage<THorizontalDepthProfile> message)
        {
            this.Model.UpdateWithHdp(message.State, this.maximumDepth, this.resolutionInMetersPerPixel, Colors.Red, Colors.White, Colors.Gray);
            return true;
        }
    }
}
